package com.ruoyi.web.Interceptord;

import com.ruoyi.common.exception.user.AntiReplayException;
import com.ruoyi.common.exception.user.IntegrityErrorException;
import com.ruoyi.common.utils.RequestBodyUtils;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@Slf4j
public class XzxInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;

        //后期可以放到同一个工具类中使用
    public static String encrypt(String paramStr){
        // 将返回的hash值转换成16进制字符串
        String resultHexString = "";
        try {
            // 将字符串转换成byte数组
            byte[] srcData = paramStr.getBytes("UTF-8");
            // 调用hash()
            byte[] resultHash = hash(srcData);
            // 将返回的hash值转换成16进制字符串
            resultHexString = ByteUtils.toHexString(resultHash);
        } catch (UnsupportedEncodingException e) {
            log.info(e.toString());
        }
        return resultHexString;
    }

    public static byte[] hash(byte[] srcData){
        SM3Digest digest = new SM3Digest();
        digest.update(srcData,0,srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash,0);
        return hash;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//"/login".equals(request.getHeader("url"))  ||  "/getInfo".equals(request.getHeader("url")) ||
        //所有请求都需要判断
        //放行
//        if(request.getRequestURI().contains("getPublicKey") || request.getRequestURI().contains("insertPublicKey") || request.getRequestURI().contains("captchaImage")){//|| request.getHeader("url") == null
//            log.info("登录放行~~~~~");
//            return true;
//        }
//        log.info("当前的url是==== 》   {}",request.getRequestURI());
//
//        String uid = (String) redisTemplate.opsForValue().get("uid");
//        String wzxtime = request.getHeader("wzxtime");
//        log.info("后端接受的时间    {}" ,wzxtime);
//        String wzxdata = request.getHeader("wzxdata");
//        log.info("加密周的  " +wzxdata);
//
//        if("".equals(wzxtime) || "".equals(wzxdata)  || wzxtime ==null || wzxdata ==null){
//            //错误  throw new IntegrityErrorException();
//            log.info("三个值，总有一个是null的");
//            throw new IntegrityErrorException();
//        }
//
//        long nowTime = System.currentTimeMillis();
//        log.info("打印时间的差值是多少 {}",String.valueOf(nowTime - Long.parseLong(wzxtime)));
//        //<7>验证timestamp参数，服务端当前时间timestamp_now-timestamp是否大于60s或<-5s（默认一次HTTP请求从发出到到达服务器的时间是不会超过60s的，且不会小于0，考虑到计算机精度和多网关业务场景，故判断条件为>60s或<-5s）。若大于60s或<-5s
//        long b = nowTime - Long.parseLong(wzxtime);
//        if( b > 60000 || b < -5000 ){
//            log.info("当前的值   在 b > 60000 || b < -5 范围之间 ==>  {}  ", b );
//            throw new IntegrityErrorException();
//        }
//
//
//        log.info("当钱请求的url是  ==》 {}" ,request.getRequestURL());
//        //String data = request.getHeader("data");
//       // log.info("加密的data是  ==》 {}" ,data);
//
//        String requestBody = RequestBodyUtils.getRequestBody();
//        log.info("当钱请求中的 requestBody  ==》 {}" ,requestBody);
//       // log.info("判断请求数据是否相等  ==》 {}" ,requestBody.equals(data));
//        if(requestBody.equals("{}")){
//            log.info("这真的只是一个 {}");
//            requestBody = "";
//        }
//       /* System.out.println("");*/
//        String result = encrypt(requestBody+","+uid+","+wzxtime);
//        log.info("加密之后的hash {}",result);
//        log.info("加密之后的hash {}",result.equals(wzxdata));
//        log.info("拿出来的对象是什么  " +requestBody);
//
//
//        if(!result.equals(wzxdata)){
//            throw new IntegrityErrorException();
//        }
        return true;
    }
}
